package com.example.activity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.service.TestService;
import com.example.studyapplication.MainActivity;
import com.example.studyapplication.R;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2021/1/12
 */
public class ServiceActivity extends Activity {

    private Intent intent;
    /**
     * ServiceConnection代表与服务的链接，他只有两个方法
     * onServiceConnected 和onServiceDisonnected,
     * 前置是在操作者在连接一个服务成功是被调用，而后者是在服务崩溃活被杀死导致的链接中断时
     */
    private ServiceConnection conn;
    private TestService testService;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context,ServiceActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        conn = new ServiceConnection(){
            /**
             * 与服务器端交互的接口方法，绑定服务的时候被回调，在这个方法获取绑定service传
             * 通过这个IBinder对象，实现宿主和service的交互
             * @param name
             * @param service
             */
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.e("ServiceActivity","绑定成功调用：onServiceConnected");
                TestService.LocalBinder binder = (TestService.LocalBinder) service;
                testService = binder.getService();
            }

            /**
             * 当取消绑定的时候被回调，但征程情况下不被调用的，他的调用时机是当service
             * 被意外销毁时
             * 例如内存的资源不足时，这个方法才被自动调用
             * @param name
             */
            @Override
            public void onServiceDisconnected(ComponentName name) {
                testService = null;
            }
        };

    }

    public void startServcie(View view) {
        intent = new Intent(ServiceActivity.this, TestService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android8.0以上通过startForegroundService启动service
            startForegroundService(intent);
        } else {
            startService(intent);
        }
    }

    public void closeService(View view) {
        stopService(intent);
//        Intent intent = new Intent();
//        intent.setAction("com.close.alarm");
//        sendBroadcast(intent);
    }


    /**
     * 开启绑定
     * @param view
     */
    public void bindServcie(View view) {
        intent = new Intent(ServiceActivity.this, TestService.class);
        bindService(intent,conn, Service.BIND_AUTO_CREATE);
    }


    /**
     * 解除绑定
     * @param view
     */
    public void unBindServcie(View view) {
      if(testService != null){
          testService = null;
          unbindService(conn);
      }
    }

    /**
     * 隐式启动
     * @param view
     */
    public void implicitStart(View view) {
        intent = new Intent();
        intent.setAction("com.example.service.TestService");
        //加上这句话可以“隐式”启动
        intent.setPackage(getPackageName());
        startService(intent);
    }

    /**
     * 解除绑定
     * @param view
     */
    public void getData(View view) {

        if(testService != null){
            Log.e("ServiceActivity",testService.getData()+"");
        } else {
            Log.e("ServiceActivity","还没绑定呢，先绑定，无法从服务端获取数据");
        }

    }
}
